package ModelLayer;
import java.util.ArrayList;


/**
 * 
 * @Author (Alexander Hvidberg, Dennis Kirkebak & Thomas Kulmbach)
 * @Team (Group 1)
 * @Version (08/11/13)
 * 
 */
public class DVDContainer
{
    private static DVDContainer instance;
    private ArrayList<DVD> dvds;
    private int lastId;
    private int lastSerial;

    /**
     * Constructor for objects of class DVDContainer
     */
    private DVDContainer()
    {
        dvds = new ArrayList<DVD>();
        lastId = 1;
        lastSerial = 1;
    }
    
    /**
     * Singleton
     */
    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }
    
    /**
     * Create a new DVD and add it to the collection.
     * 
     * @param String title - the title of the dvd
     * @param String artist - the artist of the dvd
     * @param String releaseDate - the release date
     * @param String aqDate - the acquisition date
     * @param double aqPrice - the acquisition price
     */
    public void createDVD(String title, String artist, String releaseDate, String aqDate, double aqPrice)
    {
        dvds.add(new DVD(lastId, title, artist, releaseDate,lastSerial, aqDate, aqPrice));
        lastId++;
        lastSerial++;
    }
    
    /**
     * Find a DVD in the collection.
     * 
     * @param int id - the id of the dvd
     * 
     * @return DVD d - the found dvd
     */
    public DVD findDVD(int id)
    {
        int index = 0;
        DVD d = null;
        boolean found = false;
        while(index<dvds.size() && !found) {
            if(id==dvds.get(index).getId()) {
                d = dvds.get(index);
                found = true;
            }
            else {
                index++;
            }
        }
        return d;        
    }
    
    /**
     * Create a DVDArticle and add it to a DVD with the given id.
     * 
     * @param int id - the id of the article
     * @param String aqDate - the acquisition date
     * @param double aqPrice - the acquisitio price
     */
    public void createDVDArticle(int id, String aqDate, double aqPrice)
    {
        DVD d = findDVD(id);
        if(d!=null) {
            d.createDVDArticle(lastSerial, aqDate, aqPrice);
            lastSerial++;
            System.out.println("Copy has been created. \n ");
        }
        else {
            System.out.println("There is no DVD with this ID. \n ");
        }
    
    }
    
    /**
     * Update the information about a DVD with a given id
     * 
     * @param int id - the id of the dvd
     * @param String title - the title of the dvd
     * @param String artist - the artist of the dvd
     * @param String releaseDate - the release date
     */
    public void updateDVD(int id, String title, String artist, String releaseDate)
    {
        DVD d = findDVD(id);
        d.updateDVD(title, artist, releaseDate);
    }
    
    /**
     * Delete a DVD with a given id
     * 
     * @param int id - the id of the dvd
     */
    public void deleteDVD(int id)
    {
        DVD d = findDVD(id);
        if (d!=null) {
            dvds.remove(d);
            System.out.println("DVD has been deleted. \n ");
        }
        else {
            System.out.println("There is no DVD with this ID. \n");
        }
    }
    
    /**
     * Delete a DVDArticle from a given dvd
     * 
     * @param int id - the id of the dvd
     * @param int serialNumber - the serialnumber of the article
     */
    public void deleteDVDArticle(int id, int serialNumber)
    {
        DVD d = findDVD(id);
        d.deleteDVDArticle(serialNumber);
    }
    
    /**
     * Print a list of articles
     * 
     * @param int id - the id of the dvd
     */
    public void printArticles(int id)
    {
        int index = 0;
        boolean found = false;
        while(index<dvds.size() && !found) {
            if(id==dvds.get(index).getId()) {
                dvds.get(index).printArticles();
                found = true;
            }
            else {
                index++;
            }
        }
    
        
    }
    
    /**
     * Show a list of all DVDs
     * 
     * @return DVD d - a list of dvds
     */
    public ArrayList getDVDList() {
        return dvds;
    }
    
    /**
     * Print a list with the name of the DVDs in the collection.
     */
    public void printDVDList()
    {
        for(DVD dvd : dvds) {
            System.out.println("ID: "+dvd.getId()+"  Title: "+dvd.gettitle());
        }
    }
}
